<template>
  <div>
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in jueSeList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="confirm">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoleAPI } from '@/api/setting'
import { getYGXinXiAPI } from '@/api/user'
import { feiPeiJueSeAPI } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    // 父组件传来的用户id
    userId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      checkList: [], // 多选框组,列表权限
      jueSeList: [] // 角色列表
    }
  },
  created() {
    // 调用获取角色列表及权限的方法
    this.getJueSe()
    // 调用获取角色详情的方法
    this.getUserFn()
  },
  methods: {
    // 确认按钮 --- 点击确定，调用接口，完成角色分配
    async confirm() {
      const res = await feiPeiJueSeAPI(
        {
          id: this.userId,
          roleIds: this.checkList
        }
      )
      // console.log(res)
      this.$message.success(res.message)
      // 重新获取角色列表数据
      this.getJueSe()
      // 弹窗关闭
      this.$emit('close')
    },

    // 获取角色列表及权限
    async getJueSe() {
      const res = await getRoleAPI()
      // console.log(res)
      this.jueSeList = res.data.rows
    },

    // 获取角色详情
    async getUserFn() {
      const res = await getYGXinXiAPI(this.userId)
      // 当前的角色权限
      this.checkList = res.data.roleIds
    },

    // 取消弹窗
    cancleDialog() {
      this.$emit('close')
    }
  }
}
</script>

<style>
.footer{
  margin-top: 20px;
}
</style>
